<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="../include/taglib.jsp"%>
<%@ page import = "java.util.*" %>
<%@ page import = "com.sinodata.bsm.common.vo.ResViewRel" %>
<%
List<ResViewRel>  relList = (List<ResViewRel>)request.getAttribute("relList");
List<Long>  notDeleteList = (List<Long>)request.getAttribute("notDeleteList");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>resource-view-config-update</title>
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/form.jsp"%>
	<%@ include file="../include/tree.jsp"%>
  <script type="text/javascript">
  function add(){
	  //提交
	$.ajax({
		   type: "POST", 
		   url: "${pageContext.request.contextPath}/config/resource-view-config!save.action", //请求的action
		   data: $("#myform").serialize(), //传的参数
		   dataType: 'json',
		   success: function(data){ //结果
			   if(data == true){
				   var pWin = getTopWin(window).currentWin["update-resource-view-config"];
				   pWin.datagrid();
				   getTopWin(window).closejBox("update-resource-view-config");
			   }
			   else{
				   getTopWin(window).getMessageBox().alert("此配置已经被删除，请返回并刷新列表","提示");
			   }
		   }
	});
  }
 var resRelTreeConfig = {
			async: {
				enable: true,
				url:"${pageContext.request.contextPath}/resource/res-frame!resViewTree.action",
				dataType: "text",
				autoParam:["id"]
			},
			check: {
				enable: true
			},
			callback: {
				onClick: relNodeClick,
				onNodeCreated: zTreeOnAsyncSuccess,
				onCheck: zTreeOnCheck
				
				
			},
			view: {
				dblClickExpand: false,
				showLine: false
			}

			
	};
		
 	function zTreeOnCheck(event, treeId, treeNode) {
    	setResValue();
	};
	function setResValue()
	{
			var rTree = $.fn.zTree.getZTreeObj("relRelTree");
    		var nodes = rTree.getCheckedNodes(true);
    		var ids = "";
    		var names = "";
    		
    		for(var i=0; i<nodes.length; i++){
    				n = nodes[i];
    				names += (i==0? n.name : (","+n.name));
    				var arr = n.id.split("|");
					ids += (i==0? arr[1] : (","+arr[1]));
    				
    		}
    			$("#resRelName").val(names);
  				$("#resRelId").val(ids);
  
 
	}
 			
	function zTreeOnAsyncSuccess(event, treeId, n) {
		try{
			var rTree = $.fn.zTree.getZTreeObj(treeId);
				if(n.id.substring(0,1)==3){
  					n.nocheck = true;
  					rTree.updateNode(n);
  				}else{
  					if(n.level==1 && map[n.id]!=null){
  						n.checked = true;
  						if(noDeleteMap[n.id]!=null){
  							rTree.setChkDisabled(n, true);
  						}
  					    setResValue();
  						rTree.updateNode(n);
  					}
  				}
  				
 				
   			}catch(e){
 				
 			}
	};
	
	function showRelTree() {
			var cityObj = $("#resRelName");
			var cityOffset = $("#resRelName").offset();
		
			$("#relContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
			$("body").bind("mousedown", onRelTreeDown);
		}

		function onRelTreeDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "resRelId" || event.target.id == "relContent" || $(event.target).parents("#relContent").length>0)) {
				$("#relContent").fadeOut("fast");
				$("body").unbind("mousedown", onRelTreeDown);
			}
		}
		function relNodeClick(e, treeId, treeNode) {
			$("#relContent").fadeOut("fast");
			$("body").unbind("mousedown", onRelTreeDown);
	
			
		}
		var map = new Array();
		var noDeleteMap = new Array();
  $(function(){
  		
  		<%
  			for(ResViewRel rel : relList){
  		%>
  			map["2|<%=rel.getResId()%>|0"]="<%=rel.getResId()%>";
  		<%		
  			}
  		%> 
  		<%
  			for(Long id : notDeleteList){
  		%>
  			noDeleteMap["2|<%=id%>|0"]="<%=id%>";
  		<%		
  			}
  		%> 
  		
  		
  		
  	  type =  { "Y" : "", "N" : "" };
  	  $.fn.zTree.init($("#relRelTree"),resRelTreeConfig);
  	  var zTree = $.fn.zTree.getZTreeObj("relRelTree");
  	  zTree.setting.check.chkboxType = type;
	  $.formValidator.initConfig({formid:"myform"
          ,onerror:function(msg){}
          ,onsuccess:function(){
        	  		add();
          			return false;
          }
          });
	  $("#expandLevel").formValidator({onshow:"请输入数字 1-9",onfocus:"请输入数字 1-9",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:1,onerror:"长度非法"})
	  .functionValidator({
			    fun:function(val,elem){
			    	 var reg = /^[0-9]+$/;
			    	    if(!reg.test(val)){
			    	        return "格式不正确";
			    	    }
			    	    return true;
				}
	  });
	  $("#name").formValidator({onshow:"请输入字符串  长度(1-64)",onfocus:"请输入字符串  长度(1-64)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"})
	  .functionValidator({
		    fun:function(val,elem){
		    	if(val.replace(/[ ]/g, "") == ''){
	    	    	return "不能为空字符串";
	    	    }
		    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
		    	        return "长度超出最大范围";
		    	    }
		    	    return true;
			}
});
  });
  function closewin(){
	  getTopWin(window).closejBox("update-resource-view-config");
  }
  function pageReset(){
	  $("#myform").get()[0].reset(); 
	}
  </script>
  <style type="text/css">
		.ztree {margin-top: 5px;border: 1px solid #617775;background: #f0f6e4;width:220px;height:250px;overflow-y:scroll;overflow-x:auto;}
		.hover {background:url(../style/default/bsm/images/downArrow-hover.gif) no-repeat top left; cursor:pointer;}
	</style>
  </head>
  
  <body style="overflow-y:hidden;"  class="padding-8">
    	<form  id="myform" name="myform" method="post">
    		<table class="table_eidt">
    			<tr>
	    			<td class="td_left" width="100px"><span class="span_alert">*</span>视图名称</td>
	    			<td class="td_right"><input type="text" name="name" id="name" value="${requestScope.resView.name}" class="input_text">
	    			<span id="nameTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left"><span class="span_alert">*</span>打开层数</td>
	    			<td class="td_right"><input type="text" name="expandLevel" id="expandLevel" value="${requestScope.resView.expandLevel}" class="input_text">
	    			<span id="expandLevelTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
	    			<td class="td_left">顶级资源关联</td>
	    			<td class="td_right">
	    			<input style="float:left; width:202px;" type="text" name="resRelName" id="resRelName"  class="input_text tree_res" />
					<input type="hidden" name="resRelId" id="resRelId"  />
					<span class="hover" style="width:21px; height:25px; display:block; background:url(../style/default/bsm/images/downArrow.gif) no-repeat top left; float:left;" onfocus="showRelTree(); return false;" ></span>
	    			<span id="expandLevelTip" style="width:144px; float:left; margin-left:7px;"></span></td>
    			</tr>
    		</table>
    		<div align="center"  style="margin-top:5px;">
	    		<input type="submit" id="button" name="button" value="保存" class="btn-4">
	    		<input type="button" class="btn-4" onclick="pageReset();" value="重置">
	    		<input type="button" value="取消" onclick="closewin();" class="btn-4">
    		</div>
    		<input type="text" name="id" id="id" value=${requestScope.resView.id} style="visibility: hidden;">
    	</form>
     <div id="relContent" class="menuContent" style="display:none; position: absolute;">
	  <ul id="relRelTree" class="ztree" style="margin-top:0;padding-left:0;  width:220px; height:155px;  "></ul>
   </div>
  </body>
</html>
